package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.CurrentAffairsTypeDao;
import com.sc.nft.entity.CurrentAffairsType;
import com.sc.nft.entity.dto.CurrentAffairsTypeDTO;
import com.sc.nft.entity.vo.CurrentAffairsTypeVo;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.CurrentAffairsTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 资讯类型表服务接口实现
 *
 * @author zhangchao
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-03-06 17:16:12
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CurrentAffairsTypeServiceImpl implements CurrentAffairsTypeService {
    private final CurrentAffairsTypeDao currentAffairsTypeDao;

    @Override
    public Page<CurrentAffairsTypeVo> currentAffairsTypeVoPage(CurrentAffairsTypeDTO currentAffairsTypeDTO) {
        return currentAffairsTypeDao.currentAffairsTypeVoPage(currentAffairsTypeDTO);
    }

    @Override
    public void currentAffairsTypeUpdate(CurrentAffairsType currentAffairsType) {
        //校验模块名称
        Integer count = currentAffairsTypeDao.checkModuleNameUpdate(currentAffairsType.getCurrentAffairsType(), currentAffairsType.getId());
        Assert.isFalse(ObjectUtil.isNotNull(count) && count >= 1, () -> new GlobalRunTimeException("模块名称不允许重复"));
        currentAffairsTypeDao.currentAffairsTypeUpdate(currentAffairsType);
    }

    @Override
    public void currentAffairsTypeAdd(CurrentAffairsType currentAffairsType) {
        Integer count = currentAffairsTypeDao.checkModuleNameAdd(currentAffairsType.getCurrentAffairsType());
        //校验模块名称
        Assert.isFalse(ObjectUtil.isNotNull(count) && count >= 1, () -> new GlobalRunTimeException("模块名称不允许重复"));
        currentAffairsType.setIsOpen(Boolean.FALSE);
        Assert.isTrue(currentAffairsType.insert(),()->new GlobalRunTimeException("新增失败！"));
    }

    @Override
    public void currentAffairsTypeDelete(Long id) {
        CurrentAffairsType currentAffairsTypeDaoById = currentAffairsTypeDao.getCurrentAffairsTypeById(id);
        Assert.isFalse(Boolean.TRUE.equals(currentAffairsTypeDaoById.getIsOpen()), () -> new GlobalRunTimeException("启用中禁止删除"));
        Assert.isTrue(currentAffairsTypeDao.currentAffairsTypeDelete(id), () -> new GlobalRunTimeException("删除失败！"));
    }

    @Override
    public void currentAffairsTypeShow(Long id, Boolean switchRequest) {
        CurrentAffairsType currentAffairsTypeDaoById = currentAffairsTypeDao.getCurrentAffairsTypeById(id);
        Assert.isTrue(ObjectUtil.isNotNull(currentAffairsTypeDaoById), () -> new GlobalRunTimeException("该模块类型不存在！"));
        Assert.isTrue(currentAffairsTypeDao.currentAffairsTypeShow(id, switchRequest), () -> new GlobalRunTimeException("更新失败！"));
    }

    @Override
    public List<CurrentAffairsTypeVo> getCurrentAffairsTypeVo() {
        return currentAffairsTypeDao.getCurrentAffairsTypeVo();
    }

    @Override
    public List<CurrentAffairsTypeVo> getCurrentAffairsTypeVoIsOpen() {
        return currentAffairsTypeDao.getCurrentAffairsTypeVoIsOpen();
    }
}